layui.use(['table', 'jquery', 'layer'], function (data) {
    var table = layui.table,
        $ = layui.jquery,
        layer = layui.layer;

    var tableIns = table.render({
        elem: "#warehouseOut",
        height: 'full-125',
        url: ctx + '/production/selectOutMaterial',
        page: true,
        cellMinWidth: 80, // 单元格最小宽度
        toolbar: '#toolbarDemo',
        cols: [[// 表头
            {type: 'checkbox'},
            {type: "numbers"},
            {
                title: "关联项目", field: "projectNo", templet: function (data) {
                    return data.projectBean.projectNo;
                }
            },
            {
                title: "领料单号", field: "pickNo", templet: function (data) {
                    if (data.pickNo != null) {
                        return data.pickNo;
                    }
                    if (data.pickNo == null) {
                        return '';
                    }

                }
            },
            {
                title: "物料编码", field: "materialNo", templet: function (data) {
                    return data.materialBean.materialNo
                }
            }
            , {
                title: "物料名称", field: "materialName", templet: function (data) {
                    return data.materialBean.materialName

                }
            },
            {
                title: "出库数量", field: "materialCount"
            }
            , {
                title: "领料类型", field: "pickType", templet: function (data) {
                    if (data.pickType == "0") {
                        return "正常领料";
                    }
                    if (data.pickType == "1") {
                        return "生产补料";
                    }
                }
            }
            , {
                title: "领料人", field: "empName", templet: function (data) {
                    return data.empBean.empName;
                }

            }
            , {
                title: "描述", field: "pickDescribe", templet: function (data) {
                    if (data.pickDescribe == null) {
                        return '';
                    }
                    if (data.pickDescribe != null) {
                        return data.pickDescribe;
                    }
                }
            }, {
                title: "操作", templet: "#tool"
            }
        ]]
    })

    table.on('tool(warehouseOutFilter)', function (data) {
        if (data.event == "outMaterial") {
            if (data.data.pickNo != null && data.data.pickNo != '') {
                layer.msg("重复发料");
                console.log(data.data.pickNo)
                return;
            } else if (data.data.pickType == 1) {
                layer.msg("审批中")
                return;
            }
            layer.confirm("确认发料吗", {icon: 5, title: "仓库发料"}, function (index) {
                layer.close(index);
                $.ajax({
                    type: "post",
                    data: {
                        id: data.data.id
                    },
                    dataType: "json",
                    url: ctx + "/production/outUpdateWarehouse",
                    success: function (data) {
                        if (data.code == 200) {
                            layer.msg(data.msg);
                            tableIns.reload();
                        } else {
                            layer.msg(data.msg)
                        }
                    },
                    error: function (data) {
                        layer.msg(data.msg)
                    }
                })
            })
        }
    })
    // 监听头
    table.on('toolbar(warehouseOutFilter)', function (data) {
        if (data.event == "assign") {
            var checkStatus = table.checkStatus(data.config.id);
            var data = checkStatus.data;
            openAssignDialog(data);
        }
    })

    function openAssignDialog(data) {
        if (data.length < 1) {
            layer.msg("请选择审批订单", {icon: 6})
            return;
        }
        if (data.length > 1) {
            layer.msg("单个审批", {icon: 6})
            return;
        }
        if (data[0].pickType != 1) {
            layer.msg("无需审批哦", {icon: 6})
            return;
        }

        layer.confirm("确认审批通过吗", {icon: 5, title: "审批补料"}, function (index) {
                layer.close(index);
                $.ajax({
                    type: "post",
                    url: ctx + "/production/assignSupp",
                    data: {
                        id: data[0].id
                    },
                    success: function (data) {
                        if (data.code == 200) {
                            layer.msg(data.msg);
                            tableIns.reload();
                        } else {
                            layer.msg(data.msg)
                        }
                    }, error: function (data) {
                        layer.msg(data.msg)
                    }

                })
            }
        )


    }


})

